<template>
  <div class="chart-container top-middle-cmp">
    <div class="chart-name">
      用户数量月趋势
      <dv-decoration-3 style="width: 200px; height: 20px" />
    </div>
    <div class="chart" style="height: 360px" ref="chart"></div>
  </div>
</template>

<script>
import Charts from "@jiaminghi/charts";
export default {
  name: "UserNumberTrend",
  data() {
    return {
      chart: null,
      option1: {
        grid: {
          style: {
            color: "#fff",
          },
        },
        xAxis: {
          name: "日期",
          axisLine: {
            lineStyle: {
              color: "#fff",
            },
          },
          data: [
            "10/01",
            "10/02",
            "10/03",
            "10/04",
            "10/05",
            "10/06",
            "10/07",
            "10/08",
            "10/09",
            "10/10",
            "10/11",
            "10/12",
            "10/13",
            "10/14",
            "10/15",
          ],
        },
        yAxis: {
          name: "用户数量",
          data: "value",
          nameTextStyle: {
            fill: "#fff",
          },
          axisLine: {
            style: {
              stroke: "#fff",
            },
          },
          axisLabel: {
            style: {
              fill: "#fff",
            },
          },
        },
        series: [
          {
            data: [
              120, 132, 101, 134, 90, 230, 210, 180, 210, 220, 200, 190, 230,
              240, 250,
            ],
            type: "line",
            lineArea: {
              show: true,
              gradient: ["rgba(251, 114, 147, 0.6)"],
            },
            lineStyle: {
              stroke: "rgba(251, 114, 147, 1)",
            },
            linePoint: {
              radius: 4,
            },
          },
          {
            data: [
              150, 182, 121, 154, 110, 260, 230, 190, 220, 250, 220, 210, 260,
              270, 280,
            ],
            type: "bar",
            label: {
              show: true,
            },
            barStyle: {
              stroke: "rgba(103, 224, 227, 1)",
            },
          },
        ],
      },
    };
  },
  mounted() {
    this.chart = new Charts(this.$refs.chart);
    this.initChart();
  },
  methods: {
    initChart() {
      let index = 0;
      let option1 = this.option1;
      this.chart.setOption(option1);
      setInterval(() => {
        index++;
        if (index > 1) {
          index = 0;
        }
        let insertData = Math.floor(Math.random() * 200) + 100;
        option1.series[0].data.shift();
        option1.series[0].data.push(insertData);
        option1.series[1].data.shift();
        option1.series[1].data.push(insertData);
        this.chart.setOption(option1);
      }, 4000);
    },
  },
};
</script>

<style lang="scss">
.top-middle-cmp {
  position: relative;
  padding: 0 50px;
  box-sizing: border-box;

  .chart-name {
    position: absolute;
    right: 70px;
    text-align: right;
    font-size: 20px;
    top: 10px;
  }

  .chart {
    width: 100%;
    height: 400px;
  }
}
</style>
